import os
from myerrors import NoMore
from actions import Sign
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users
from modelo import Greeting


class MainPage(webapp.RequestHandler):
    """
       The Main Page Handler. The default entry.
    """
    def get(self):
        """
        The default entry
        """
        greetings = Greeting().gql("ORDER BY date desc LIMIT 6")
        if (users.get_current_user()):
          link_text = "Logout"
          link = users.CreateLogoutURL(self.request.uri)
        else:
          link_text = "Login"
          link = users.CreateLoginURL(self.request.uri)

        # Localizamos el fichero de plantilla
        template_values = {
          'greetings' : greetings,
          'link'  : link,
          'link_text':link_text,
        }
        path = os.path.join(os.path.dirname(__file__), "index.html")
        self.response.out.write(template.render(path, template_values))



application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/sign', Sign),
                                      ('/nomore', NoMore)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()